﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;

namespace WarehouseService
{
    
    public class Warehouse : IWarehouseService
    {

        public Parts[] GetParts()
        {

            Parts[] parts;
            List<Parts> parts_list = new List<Parts>();

            SqlConnection connection = new SqlConnection(
                "Data Source=fastapps04.qut.edu.au;Initial Catalog=n7251998;User ID=n7251998;Password=inb374");
            connection.Open();

            SqlCommand command = new SqlCommand("SELECT ID, Name, Price, Quantity, Ordertime FROM Parts", connection);

            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read()) {

                Parts temp = new Parts(reader.GetInt32(0), reader.GetString(1), reader.GetInt32(2), reader.GetInt32(3), reader.GetInt32(4));
                parts_list.Add(temp);
            }
            
            parts = parts_list.ToArray();
            reader.Close();
            connection.Close();

            return parts;
        }




        public void setQuantityPart(string name, int quantity)
        {
            SqlConnection connection = new SqlConnection(
                "Data Source=fastapps04.qut.edu.au;Initial Catalog=n7251998;User ID=n7251998;Password=inb374");
            connection.Open();

            SqlCommand command = new SqlCommand("SELECT ID, Name, Price, Quantity, Ordertime FROM Parts", connection);

            string updateQuery = @"UPDATE Parts SET Quantity = " + quantity + " WHERE  (name = '" + name + "')";
            command.CommandText = updateQuery;
            command.ExecuteNonQuery();

            connection.Close();
        }


        public int CalculateOrderPrice(String[] parts)
        {

            int total_price = 0;

            List<String> list = new List<String>();

            list = parts.ToList<String>();

            SqlConnection connection = new SqlConnection(
                "Data Source=fastapps04.qut.edu.au;Initial Catalog=n7251998;User ID=n7251998;Password=inb374");
            connection.Open();

            SqlCommand command;
            SqlDataReader reader;

            foreach (String part in list) {

                string query = string.Format("SELECT Price FROM Parts WHERE Name = '{0}'", part);
                command = new SqlCommand(query,connection);
                reader = command.ExecuteReader();

                while (reader.Read()) {

                    int temp = reader.GetInt32(0);
                    total_price = total_price + temp;
                }
                
                reader.Close();    

            }

            return total_price;

        }


        public int checkStockPart(string name)
        {

            int in_stock = 0;
            
            SqlConnection connection = new SqlConnection(
                "Data Source=fastapps04.qut.edu.au;Initial Catalog=n7251998;User ID=n7251998;Password=inb374");
            connection.Open();

            SqlCommand command;
            SqlDataReader reader;

            string query = string.Format("SELECT Quantity FROM Parts WHERE Name = '{0}'", name);
            command = new SqlCommand(query, connection);
            reader = command.ExecuteReader();

            while (reader.Read()) {

                in_stock = reader.GetInt32(0);
            }

            reader.Close();
            connection.Close();

            return in_stock;
        }
    }
}
